<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分润时间" >
        <el-date-picker clearable
                        v-model="dateRange"
                        type="datetimerange"
                        range-separator="至"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        :picker-options="pickerOptions"
                        start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ecs:separateAccounts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="separateAccountsList"   :span-method="objectSpanMethod">
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="二级商户号" align="center" prop="subMerchantId" />
      <el-table-column label="账号昵称" align="center" prop="platformBalance" width="260">
        <template slot-scope="{ row }">
          <span v-if="row.roleType === 1">{{ row.platformNickName}}</span>
          <span v-if="row.roleType === 2">{{ row.agentNickName}}</span>
          <span v-if="row.roleType === 3">{{ row.shopNickName}}</span>
          <span v-if="row.roleType === 4">{{ row.childAgentNickName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="roleType">
        <template slot-scope="{ row }">
          <span v-if="row.roleType === 1">平台</span>
          <span v-if="row.roleType === 2">代理</span>
          <span v-if="row.roleType === 3">店铺</span>
          <span v-if="row.roleType === 4">二级代理</span>
        </template>
      </el-table-column>
      <el-table-column label="分润比例" align="center" prop="splittingRate" />
      <el-table-column label="分润金额" align="center" prop="splittingAmount" />
      <el-table-column label="入金金额" align="center" prop="depositRecordAmount" />
      <el-table-column label="分润时间" align="center" prop="createTime" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listSeparateAccounts } from "@/api/ecs/separateAccounts";

export default {
  name: "SeparateAccounts",
  data() {
    return {
      //日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      separateAccountsList:[],
      //时间快捷范围
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type:1,
        splitId: null,
        depositRecordId: null,
        subMerchantId: null,
        roleType: null,
        shopName: null,
        tableName: null,
        tableNameRequestId: null,
        splittingRate: null,
        splittingAmount: null,
        depositRecordAmount: null,
        incomingRate: null
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询所有三方入金后分账记录列表 */
    getList() {
      this.loading = true;
      listSeparateAccounts(this.addDateRange(this.queryParams, this.dateRange,"CreateDate")).then(response => {
        this.separateAccountsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    objectSpanMethod({ row, column, rowIndex }) {
      const currentRow = this.separateAccountsList[rowIndex];
      const previousRow = this.separateAccountsList[rowIndex - 1];

      if (column.property === 'shopName') {
        if (rowIndex > 0 && currentRow.depositRecordId === previousRow.depositRecordId) {
          return [0, 0]; // 隐藏单元格
        }

        // 计算连续的相同行数
        let rowspan = 1;
        for (let i = rowIndex + 1; i < this.separateAccountsList.length; i++) {
          if (this.separateAccountsList[i].depositRecordId === currentRow.depositRecordId) {
            rowspan++;
          } else {
            break;
          }
        }
        return [rowspan, 1]; // 合并行
      }

      // 其他列不合并
      return [1, 1];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/separateAccounts/export', {
        ...this.queryParams
      }, `分润记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

